package com.kang.until;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.kang.pojo.ReturnRequestItem;

public class ReturnAgentExcelExport extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static File buildReturnAgentExcel(HttpServletRequest request, HttpServletResponse response, 
			List<ReturnRequestItem> rriList) throws Exception  {
		//请求发送都是utf-8
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
		// 本地年月日时分秒作为文件名
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		File ExcelFile = new File(FileUtil.TEMP_PATH + "待退货物资_"+sdf.format(new Date())+".xlsx");
		try {
			// 输出流
			OutputStream os = new FileOutputStream(ExcelFile.getAbsolutePath());
			// 工作区
			XSSFWorkbook wb = new XSSFWorkbook();

			// 工作区名
			XSSFSheet sheet = wb.createSheet("待退货物资");

			// 创建表头
			XSSFRow row = sheet.createRow(0);
			row.createCell(0).setCellValue("申请单号");
			row.createCell(1).setCellValue("批次号");
			row.createCell(2).setCellValue("条码");
			row.createCell(3).setCellValue("仓库");
			row.createCell(4).setCellValue("物品名称");
			row.createCell(5).setCellValue("数量");
			row.createCell(6).setCellValue("单位");
			row.createCell(7).setCellValue("品牌");
			row.createCell(8).setCellValue("物料");
			row.createCell(9).setCellValue("邮轮");
			row.createCell(10).setCellValue("提交时间");
			
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
			
			// 遍历数据装进table
			for (int i = 0; i < rriList.size(); i++) {
				// sheet.createRow(i+1) 因为表头是0
				
				XSSFRow rows = sheet.createRow(i + 1);
				rows.createCell(0).setCellValue(rriList.get(i).getApplicationNo());
				rows.createCell(1).setCellValue(rriList.get(i).getBatchNumber());
				rows.createCell(2).setCellValue(rriList.get(i).getArticleBarCode());
				rows.createCell(3).setCellValue(rriList.get(i).getWarehouseName());
				rows.createCell(4).setCellValue(rriList.get(i).getChineseName());
				rows.createCell(5).setCellValue(rriList.get(i).getNumber());
				rows.createCell(6).setCellValue(rriList.get(i).getSalesUnit());
				rows.createCell(7).setCellValue(rriList.get(i).getBrand());
				rows.createCell(8).setCellValue(rriList.get(i).getMaterialMaterial());
				rows.createCell(9).setCellValue(rriList.get(i).getCruiseName());
				rows.createCell(10).setCellValue(sdf1.format(rriList.get(i).getCommitTime()).toString());
				
			}

			// 写文件
			wb.write(os);
			// 关闭输出流
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ExcelFile;
	}
	

}
